package com.fengwk.user.controller;

import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fengwk.support.util.ResultUtils;
import com.fengwk.user.face.annotation.Permission;
import com.fengwk.user.face.model.User;
import com.fengwk.user.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Permission("user")
@Api(tags = "用户")
@RestController
@RequestMapping("/api/user")
public class UserController {

	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;
	
	@Autowired
	private UserService userService;
	
	@ApiOperation(
			value = "更新用户", 
			response = User.class)
	@PostMapping("/updateUser")
	public void updateUser(
			@ApiParam(value = "用户id,更新键", required = true) @RequestParam(value = "userId", required = true) Long userId,
			@ApiParam(value = "用户头像id", required = false) @RequestParam(value = "avatarId", required = false) Long avatarId,
			@ApiParam(value = "用户名", required = false) @RequestParam(value = "username", required = false) String username,
			@ApiParam(value = "邮箱", required = false) @RequestParam(value = "email", required = false) String email,
			@ApiParam(value = "手机号", required = false) @RequestParam(value = "mobilePhone", required = false) String mobilePhone,
			@ApiParam(value = "密码", required = false) @RequestParam(value = "password", required = false) String password,
			@ApiParam(value = "昵称", required = false) @RequestParam(value = "nickname", required = false) String nickname,
			@ApiParam(value = "真实姓名", required = false) @RequestParam(value = "realName", required = false) String realName,
			@ApiParam(value = "身份证", required = false) @RequestParam(value = "idCard", required = false) String idCard,
			@ApiParam(value = "性别", required = false) @RequestParam(value = "sex", required = false) Integer sex,
			@ApiParam(value = "出生时间", required = false) @RequestParam(value = "birthTime", required = false) Long birthTime,
			@ApiParam(value = "状态", required = false) @RequestParam(value = "status", required = false) Integer status) {
		ResultUtils.returnJsonResult(request, response, userService.updateUser(
				userId, avatarId, username, email, mobilePhone, password, nickname, realName, idCard, sex, birthTime, status));
	}

	@ApiOperation(
			value = "同步角色", 
			response = Void.class)
	@PostMapping("/synRole")
	public void synRole(
			@ApiParam(value = "目标用户id", required = true) @RequestParam(value = "userId", required = true) Long userId,
			@ApiParam(value = "同步角色id列表", required = true) @RequestParam(value = "roleIds", required = true) Collection<Long> roleIds) {
		userService.synRole(userId, roleIds);
		ResultUtils.returnJsonResult(request, response, true);
	}
	
	@ApiOperation(
			value = "添加用户角色", 
			response = Void.class)
	@PostMapping("/addRole")
	public void addRole(
			@ApiParam(value = "添加目标用户id", required = true) @RequestParam(value = "userId", required = true) Long userId,
			@ApiParam(value = "添加的角色id列表", required = true) @RequestParam(value = "roleIds", required = true) Collection<Long> roleIds) {
		userService.addRole(userId, roleIds);
		ResultUtils.returnJsonResult(request, response, true);
	}
	
	@ApiOperation(
			value = "移除用户角色", 
			response = Void.class)
	@PostMapping("/removeRole")
	public void removeRole(
			@ApiParam(value = "移除目标用户id", required = true) @RequestParam(value = "userId", required = true) Long userId,
			@ApiParam(value = "移除的角色id列表", required = true) @RequestParam(value = "roleIds", required = true) Collection<Long> roleIds) {
		userService.removeRole(userId, roleIds);
		ResultUtils.returnJsonResult(request, response, true);
	}
	
	@ApiOperation(
			value = "通过用户id获取User", 
			response = User.class)
	@PostMapping("/getUserByUserId")
	public void getUserByUserId(
			@ApiParam(value = "用户id", required = true) @RequestParam(value = "userId", required = true) Long userId) {
		ResultUtils.returnJsonResult(request, response, userService.getUserByUserId(userId));
	}

	@ApiOperation(
			value = "通过用户id列表获取User列表", 
			response = User.class)
	@PostMapping("/listUserByUserIds")
	public void listUserByUserIds(
			@ApiParam(value = "用户id列表", required = true) @RequestParam(value = "userIds", required = true) Collection<Long> userIds) {
		ResultUtils.returnJsonResult(request, response, userService.listUserByUserIds(userIds));
	}
	
	@ApiOperation(
			value = "分页获取用户", 
			response = User.class)
	@PostMapping("/pageUser")
	public void pageUser(
			@ApiParam(value = "页码", required = true) @RequestParam(value = "pageNum", required = true) Integer pageNum,
			@ApiParam(value = "每页大小", required = true) @RequestParam(value = "pageSize", required = true) Integer pageSize) {
		ResultUtils.returnJsonResult(request, response, userService.pageUser(pageNum, pageSize));
	}
	
}
